package view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import components.MyInternalFrame;

import model.Cliente;
import dao.DAOFactory;
import dao.IClienteDAO;

public class ConsultaClienteView extends MyInternalFrame {

	private JPanel contentPane;
	private JTable table;
	private DefaultTableModel dtm;
	private JLabel lblNome;
	private JTextField textNome;
	private JButton btnBuscar;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ConsultaClienteView frame = new ConsultaClienteView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ConsultaClienteView() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 72, 414, 190);
		contentPane.add(scrollPane);
		
		dtm = new DefaultTableModel(
			new Object[][] {
//						{null, null, null}, //linha
				},
				new String[] {
						"C\u00F3digo","Nome", "Data" 
				}
			);
		
		table = new JTable( dtm );
		
		table.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				
				int row = table.getSelectedRow();
				
				Cliente c = new Cliente();
				
				//Object  col1 = dtm.getValueAt(row, 0);
				//c.setId( Integer.parseInt( dtm.getValueAt(row, 0).toString() ) );
				c.setId( (Integer) dtm.getValueAt(row, 0) );
				System.out.println( c.getId() );
				
				
				//System.out.println( row );
				
			}
		});
		scrollPane.setViewportView(table);
		
		lblNome = new JLabel("Nome:");
		lblNome.setBounds(25, 26, 46, 14);
		contentPane.add(lblNome);
		
		textNome = new JTextField();
		textNome.setBounds(71, 23, 230, 20);
		contentPane.add(textNome);
		textNome.setColumns(10);
		
		btnBuscar = new JButton("Buscar");
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//limpa a JTable
				dtm.setRowCount( 0 );
				
				IClienteDAO dao = DAOFactory.getClienteDAO();
				Cliente c = new Cliente();
				c.setNome( textNome.getText() );
				
				List<Cliente> clientes =
						dao.findClientesByName( c );
				
				for (Cliente cliente : clientes) {
					dtm.addRow( new Object[] {
						cliente.getId(),
						cliente.getNome(),
						cliente.getDtNascimento()
					});
				}
			}
		});
		btnBuscar.setBounds(320, 22, 89, 23);
		contentPane.add(btnBuscar);
	}
}



